const mongoose = require('mongoose');
const request=require('request');
const config=require('../config/base');
var base=new config();
var Order = mongoose.model('order');
//taken & notTaken
exports.changeOrderTaken = function (req, res) {
    var oId=req.params._id;
    Order.findOneAndUpdate({'_id':oId},req.body,function(err,order){
        if(err){
            res.json(err);
        }else{
            order.taken=req.body.taken;
            res.json(order);
        }
    })
}

exports.loadOrderMsg = function (req, res) {
    var oId=req.params._id;
    Order.find({'_id':oId})
        .populate({path:'merchant user',select:'-password'})
        .exec(function (err,order) {
            if(err){
                res.json(err);
            }else {
                res.json(order);
            }
        });

}

//by taken
exports.loadOrderByTaken = function (req, res) {
    var v= req.query.taken;
    var mid=req.session.mid;
    req.query.merchant=mid;
    var r=parseInt(v);
    if(!isNaN(r)){
        Order.find(req.query)
            .populate({path:'merchant user'})
            .exec(function (err,orders) {
            if(err){
                res.json(err);
            }else{
                res.json(orders);
            }
        });
    }
    else{
        Order.find({taken:{$ne:0},merchant:mid})
            .populate({path:'merchant user'})
            .sort({'orderTime':'asc'})
            .exec(function (err,orders) {
                if(err){
                    res.json(err);
                }else{
                    res.json(orders);

                }
            })
    }
}

exports.loadComplaintProcess = function (req, res) {
    console.log(base.jms.complaintDes+req.query.oid);
    request(base.jms.complaintDes+req.query.oid,function (err,response,body) {
        if(err){
            res.json(err);
        }
        else if(response.statusCode==200){
            console.log(body);
            res.send(body);
        }
    })
}








